<template>
    <div style="width: 100%;height: 100vh;background-color: darkturquoise; overflow: hidden">
        <div style="width: 400px;margin: 150px auto" >
            <div style="color: #cccccc;font-size:30px;text-align: center " >欢迎注册</div>

            <el-form :model="form" ref="form" size="normal" :rules="rules">
                <el-form-item prop="username">
                    <el-input prefix-icon="el-icon-user-solid" v-model="form.username" placeholder="请输入用户名"></el-input>
                </el-form-item>

                <el-form-item prop="password">
                    <el-input prefix-icon="el-icon-lock" v-model="form.password" show-password placeholder="请输入密码"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-input prefix-icon="el-icon-lock" v-model="form.confirm" show-password placeholder="请输入密码"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button style="width: 100%" type="primary" @click="register">注 册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>

    import request from "@/utils/request";

    export default {
        name: "Login",
        data(){
            return{
                form:{

                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur'}
                    ]
                }
            }

        },
        methods:{
            register() {
                if (this.form.password != this.form.confirm ){
                    this.$message({
                        type:"error",
                        message:'两次密码输入不一致',
                    })
                    return

                }

                this.$refs['form'].validate((valid) => {//验证这个规则合法才会发送这个请求
                    if (valid) {
                        request.post("/user/register",this.form).then(res=>{
                                if (res.code == '0'){
                                    this.$message({
                                        type:"success",
                                        message:"注册成功"
                                    })
                                    this.$router.push("/login")//跳转到登录页面
                                }else {
                                    this.$message({
                                        type:"error",
                                        message:res.msg
                                    })
                                }
                            }
                        )
                    }
                });
            }
        }
    }
</script>

<style scoped>

</style>